package com.shop.item.atom.atomsvc.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.common.entity.item.Category;
import com.shop.common.model.InputBean;
import com.shop.common.model.ServiceBean;
import com.shop.common.model.param.item.ItemExtInput;
import com.shop.common.model.param.item.ItemFootInput;
import com.shop.common.model.result.item.ItemExtOutput;
import com.shop.common.model.result.item.ItemFootOutput;
import com.shop.common.tmpl.BaseService;
import com.shop.common.util.DateUtil;
import com.shop.common.util.state.DataStatus;
import com.shop.item.atom.atomsvc.IItemExtAtomService;
import com.shop.item.atom.dao.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ItemExtAtomService extends BaseService implements IItemExtAtomService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ItemFootMapper itemFootMapper;
    @Autowired
    private ItemExtMapper extMapper;


    @Override
    public ServiceBean<Integer> insertItemExt(ItemExtInput itemExt) {
        if (opSuccess(extMapper.insertSelective(itemExt))) {
            return resultSuccess();
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<String> deleteItemExt(ItemExtInput itemExt) {
        itemExt.setDataStatus(DataStatus.DELETE.value());
        int num = extMapper.updateByPrimaryKeySelective(itemExt);
        if (opSuccess(num)) {
            return resultSuccess();
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<ItemExtOutput> selectItemExt(ItemExtInput itemExt) {
        ItemExtOutput itemExtOutput = extMapper.selectBySelective(itemExt);
        if (itemExtOutput == null) {
            return resultFailed();
        }
        return resultData(itemExtOutput);
    }

    @Override
    public ServiceBean<String> updateItemExt(ItemExtInput itemExtInput) {
        int num = extMapper.updateByPrimaryKeySelective(itemExtInput);
        if (opSuccess(num)) {
            return resultSuccess();
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<Category> getCategory(Category category) {
        return resultData(categoryMapper.selectByPrimaryKey(category.getCtgId()));
    }


    @Override
    public ServiceBean<Integer> insertItemFoot(ItemFootInput itemFootInput) {
        List<ItemFootOutput> list = itemFootMapper.selectList(itemFootInput);
        if (list != null && list.size() > 0) {
            ItemFootOutput itemFootOutput = list.get(0);
            itemFootInput.setCreateTime(DateUtil.newNowDate());
            itemFootInput.setId(itemFootOutput.getId());
            if (opSuccess(itemFootMapper.updateByPrimaryKeySelective(itemFootInput))) {
                return resultSuccess();
            }
        } else if (opSuccess(itemFootMapper.insertSelective(itemFootInput))) {
            return resultSuccess();
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<List<ItemFootOutput>> selectItemFootList(ItemFootInput itemFootInput) {
        List<ItemFootOutput> list = itemFootMapper.selectList(itemFootInput);
        if (list == null || list.size() <= 0) {
            return resultFailed();
        }
        return resultSuccess();
    }

    @Override
    public ServiceBean<List<ItemFootOutput>> selectItemFootList4P(InputBean<ItemFootInput> inputBean) {
        if (inputBean.getIndex() == null || inputBean.getPageSize() == null) {
            return resultFailed();
        }
        ItemFootInput itemFootInput = inputBean.getParam();
        PageHelper.startPage(inputBean.getIndex(), inputBean.getPageSize());
        List<ItemFootOutput> list = itemFootMapper.selectList(itemFootInput);
        PageInfo<ItemFootOutput> pageInfo = new PageInfo<>(list);
        Map<String, Object> map = new HashMap<>();
        if (pageInfo.getList() == null || pageInfo.getList().size() <= 0) {
            return resultFailed();
        }
        ServiceBean serviceBean = new ServiceBean(pageInfo.getList());
        serviceBean.setMaxSize(pageInfo.getTotal());
        serviceBean.setSuccess(true);
        return serviceBean;
    }

    @Override
    public ServiceBean<String> deleteItemFoot(ItemFootInput itemFootInput) {
        if (opSuccess(itemFootMapper.deleteByPrimaryKey(itemFootInput.getId()))) {
            return resultSuccess();
        }
        return resultFailed();
    }
}
